

class Individual:
    def __init__(self,genome,generation,name=None,invertFunc=None):
        from random import random 
        
        self.id = random()
        
        if (name == None):
            self.name = str(self.id)
        else:
            self.name = name
        
        if (invertFunc == None):
            self.invertFunc = self.simpleInvert
        else:
            self.invertFunc = invertFunc
        
        self.genome = genome
        self.generation = generation
        
    def length(self):
        return len(self.genome)
    
    def invert(self,index):
        """
        Invert the given index of the Individual's genome using
        the invertFunc that was provided in the constructor
        """
        self.genome[index] = self.invertFunc(self.genome[index])
        
    def simpleInvert(self,x):
        if (x == 0):
            return 1
        else:
            return 0
    
    def toString(self):
        return "Individual " + self.name + " from gen " + str(self.generation) + " with genome " + str(self.genome)
    